class Participant(object):
    index_in_pages = None
    index_in_pages_name = None
    payer_no = None

    gf_up_curr_1 = None
    gf_up_curr_2 = None
    gf_up_curr_3 = None
    gf_up_curr_4 = None
    gf_up_curr_5 = None
    gf_up_curr_6 = None
    gf_up_curr_7 = None
    gf_up_curr_8 = None
    gf_up_curr_9 = None
    gf_up_curr_10 = None

    gf_up_next_1 = None
    gf_up_next_2 = None
    gf_up_next_3 = None
    gf_up_next_4 = None
    gf_up_next_5 = None
    gf_up_next_6 = None
    gf_up_next_7 = None
    gf_up_next_8 = None
    gf_up_next_9 = None
    gf_up_next_10 = None

    gf_down_curr_1 = None
    gf_down_curr_2 = None
    gf_down_curr_3 = None
    gf_down_curr_4 = None
    gf_down_curr_5 = None
    gf_down_curr_6 = None
    gf_down_curr_7 = None
    gf_down_curr_8 = None
    gf_down_curr_9 = None
    gf_down_curr_10 = None

    gf_down_next_1 = None
    gf_down_next_2 = None
    gf_down_next_3 = None
    gf_down_next_4 = None
    gf_down_next_5 = None
    gf_down_next_6 = None
    gf_down_next_7 = None
    gf_down_next_8 = None
    gf_down_next_9 = None
    gf_down_next_10 = None

    gf = None

    listen_advice = None
    listen_advice_help = None

    cs_part_2_1 = None
    cs_part_2_2 = None
    cs_part_2_3 = None
    cs_part_2_4 = None
    cs_part_2_5 = None
    cs_part_2_6 = None
    cs_part_2_7 = None
    cs_part_2_8 = None
    cs_part_2_9 = None
    cs_part_2_10 = None
    cs_part_2_11 = None

    cs_part_3_1 = None
    cs_part_3_2 = None
    cs_part_3_3 = None
    cs_part_3_4 = None
    cs_part_3_5 = None
    cs_part_3_6 = None
    cs_part_3_7 = None
    cs_part_3_8 = None
    cs_part_3_9 = None
    cs_part_3_10 = None
    cs_part_3_11 = None

    cs = None

    ioc_1 = None
    ioc_2 = None
    ioc_3 = None
    ioc_4 = None
    ioc_5 = None
    ioc_6 = None
    ioc_7 = None
    ioc_8 = None
    ioc_9 = None
    ioc_10 = None

    ioc_11 = None
    ioc_12 = None

    ioc = None

    birth_date = None
    age = None
    gender = None
    education = None
    education_cn = None
    investment_year = None
    investment_experience = None

    contact_info = None

    @staticmethod
    def parse_from_csv(csv_file):
        """
        从CSV文件解析参与者数据并返回Participant对象列表
        :param csv_file: CSV文件路径
        :return: 包含所有Participant对象的列表
        """
        import csv
        participants = []

        with open(csv_file, 'r', encoding='utf-8') as f:
            reader = csv.DictReader(f)
            for row in reader:
                p = Participant()

                # 解析基础信息
                p.index_in_pages = row.get('participant._index_in_pages')
                p.index_in_pages_name = row.get('participant._current_page_name')

                p.payer_no = row.get('gambler_fallacy_id.1.player.payer_no')
                if not p.payer_no or p.index_in_pages_name != 'CsEnd':
                    continue

                p.gf_up_curr_1 = row.get('gambler_fallacy_kline_advice.1.player.gf_up_curr')
                p.gf_up_curr_2 = row.get('gambler_fallacy_kline_advice.2.player.gf_up_curr')
                p.gf_up_curr_3 = row.get('gambler_fallacy_kline_advice.3.player.gf_up_curr')
                p.gf_up_curr_4 = row.get('gambler_fallacy_kline_advice.4.player.gf_up_curr')
                p.gf_up_curr_5 = row.get('gambler_fallacy_kline_advice.5.player.gf_up_curr')
                p.gf_up_curr_6 = row.get('gambler_fallacy_kline_advice.6.player.gf_up_curr')
                p.gf_up_curr_7 = row.get('gambler_fallacy_kline_advice.7.player.gf_up_curr')
                p.gf_up_curr_8 = row.get('gambler_fallacy_kline_advice.8.player.gf_up_curr')
                p.gf_up_curr_9 = row.get('gambler_fallacy_kline_advice.9.player.gf_up_curr')
                p.gf_up_curr_10 = row.get('gambler_fallacy_kline_advice.10.player.gf_up_curr')

                p.gf_up_next_1 = row.get('gambler_fallacy_kline_advice.1.player.gf_up_next')
                p.gf_up_next_2 = row.get('gambler_fallacy_kline_advice.2.player.gf_up_next')
                p.gf_up_next_3 = row.get('gambler_fallacy_kline_advice.3.player.gf_up_next')
                p.gf_up_next_4 = row.get('gambler_fallacy_kline_advice.4.player.gf_up_next')
                p.gf_up_next_5 = row.get('gambler_fallacy_kline_advice.5.player.gf_up_next')
                p.gf_up_next_6 = row.get('gambler_fallacy_kline_advice.6.player.gf_up_next')
                p.gf_up_next_7 = row.get('gambler_fallacy_kline_advice.7.player.gf_up_next')
                p.gf_up_next_8 = row.get('gambler_fallacy_kline_advice.8.player.gf_up_next')
                p.gf_up_next_9 = row.get('gambler_fallacy_kline_advice.9.player.gf_up_next')
                p.gf_up_next_10 = row.get('gambler_fallacy_kline_advice.10.player.gf_up_next')

                p.gf_down_curr_1 = row.get('gambler_fallacy_kline_advice.1.player.gf_dw_curr')
                p.gf_down_curr_2 = row.get('gambler_fallacy_kline_advice.2.player.gf_dw_curr')
                p.gf_down_curr_3 = row.get('gambler_fallacy_kline_advice.3.player.gf_dw_curr')
                p.gf_down_curr_4 = row.get('gambler_fallacy_kline_advice.4.player.gf_dw_curr')
                p.gf_down_curr_5 = row.get('gambler_fallacy_kline_advice.5.player.gf_dw_curr')
                p.gf_down_curr_6 = row.get('gambler_fallacy_kline_advice.6.player.gf_dw_curr')
                p.gf_down_curr_7 = row.get('gambler_fallacy_kline_advice.7.player.gf_dw_curr')
                p.gf_down_curr_8 = row.get('gambler_fallacy_kline_advice.8.player.gf_dw_curr')
                p.gf_down_curr_9 = row.get('gambler_fallacy_kline_advice.9.player.gf_dw_curr')
                p.gf_down_curr_10 = row.get('gambler_fallacy_kline_advice.10.player.gf_dw_curr')

                p.gf_down_next_1 = row.get('gambler_fallacy_kline_advice.1.player.gf_dw_next')
                p.gf_down_next_2 = row.get('gambler_fallacy_kline_advice.2.player.gf_dw_next')
                p.gf_down_next_3 = row.get('gambler_fallacy_kline_advice.3.player.gf_dw_next')
                p.gf_down_next_4 = row.get('gambler_fallacy_kline_advice.4.player.gf_dw_next')
                p.gf_down_next_5 = row.get('gambler_fallacy_kline_advice.5.player.gf_dw_next')
                p.gf_down_next_6 = row.get('gambler_fallacy_kline_advice.6.player.gf_dw_next')
                p.gf_down_next_7 = row.get('gambler_fallacy_kline_advice.7.player.gf_dw_next')
                p.gf_down_next_8 = row.get('gambler_fallacy_kline_advice.8.player.gf_dw_next')
                p.gf_down_next_9 = row.get('gambler_fallacy_kline_advice.9.player.gf_dw_next')
                p.gf_down_next_10 = row.get('gambler_fallacy_kline_advice.10.player.gf_dw_next')

                gf = [p.gf_up_curr_1 is not None and p.gf_up_next_1 is not None and p.gf_up_curr_1 != p.gf_up_next_1,
                      p.gf_up_curr_2 is not None and p.gf_up_next_2 is not None and p.gf_up_curr_2 != p.gf_up_next_2,
                      p.gf_up_curr_3 is not None and p.gf_up_next_3 is not None and p.gf_up_curr_3 != p.gf_up_next_3,
                      p.gf_up_curr_4 is not None and p.gf_up_next_4 is not None and p.gf_up_curr_4 != p.gf_up_next_4,
                      p.gf_up_curr_5 is not None and p.gf_up_next_5 is not None and p.gf_up_curr_5 != p.gf_up_next_5,
                      p.gf_up_curr_6 is not None and p.gf_up_next_6 is not None and p.gf_up_curr_6 != p.gf_up_next_6,
                      p.gf_up_curr_7 is not None and p.gf_up_next_7 is not None and p.gf_up_curr_7 != p.gf_up_next_7,
                      p.gf_up_curr_8 is not None and p.gf_up_next_8 is not None and p.gf_up_curr_8 != p.gf_up_next_8,
                      p.gf_up_curr_9 is not None and p.gf_up_next_9 is not None and p.gf_up_curr_9 != p.gf_up_next_9,
                      p.gf_up_curr_10 is not None and p.gf_up_next_10 is not None and p.gf_up_curr_10 != p.gf_up_next_10,
                      p.gf_down_curr_1 is not None and p.gf_down_next_1 is not None and p.gf_down_curr_1 != p.gf_down_next_1,
                      p.gf_down_curr_2 is not None and p.gf_down_next_2 is not None and p.gf_down_curr_2 != p.gf_down_next_2,
                      p.gf_down_curr_3 is not None and p.gf_down_next_3 is not None and p.gf_down_curr_3 != p.gf_down_next_3,
                      p.gf_down_curr_4 is not None and p.gf_down_next_4 is not None and p.gf_down_curr_4 != p.gf_down_next_4,
                      p.gf_down_curr_5 is not None and p.gf_down_next_5 is not None and p.gf_down_curr_5 != p.gf_down_next_5,
                      p.gf_down_curr_6 is not None and p.gf_down_next_6 is not None and p.gf_down_curr_6 != p.gf_down_next_6,
                      p.gf_down_curr_7 is not None and p.gf_down_next_7 is not None and p.gf_down_curr_7 != p.gf_down_next_7,
                      p.gf_down_curr_8 is not None and p.gf_down_next_8 is not None and p.gf_down_curr_8 != p.gf_down_next_8,
                      p.gf_down_curr_9 is not None and p.gf_down_next_9 is not None and p.gf_down_curr_9 != p.gf_down_next_9,
                      p.gf_down_curr_10 is not None and p.gf_down_next_10 is not None and p.gf_down_curr_10 != p.gf_down_next_10]
                p.gf = sum(gf)

                p.listen_advice = row.get('gambler_fallacy_kline_advice_check.1.player.advice')
                p.listen_advice_help = row.get('gambler_fallacy_kline_advice_check.1.player.help')

                p.cs_part_2_1 = row.get('cognitive_style.1.player.cs_part2_1')
                p.cs_part_2_2 = row.get('cognitive_style.1.player.cs_part2_2')
                p.cs_part_2_3 = row.get('cognitive_style.1.player.cs_part2_3')
                p.cs_part_2_4 = row.get('cognitive_style.1.player.cs_part2_4')
                p.cs_part_2_5 = row.get('cognitive_style.1.player.cs_part2_5')
                p.cs_part_2_6 = row.get('cognitive_style.1.player.cs_part2_6')
                p.cs_part_2_7 = row.get('cognitive_style.1.player.cs_part2_7')
                p.cs_part_2_8 = row.get('cognitive_style.1.player.cs_part2_8')
                p.cs_part_2_9 = row.get('cognitive_style.1.player.cs_part2_9')
                p.cs_part_2_10 = row.get('cognitive_style.1.player.cs_part2_A')
                p.cs_part_2_11 = row.get('cognitive_style.1.player.cs_part2_B')

                p.cs_part_3_1 = row.get('cognitive_style.1.player.cs_part3_1')
                p.cs_part_3_2 = row.get('cognitive_style.1.player.cs_part3_2')
                p.cs_part_3_3 = row.get('cognitive_style.1.player.cs_part3_3')
                p.cs_part_3_4 = row.get('cognitive_style.1.player.cs_part3_4')
                p.cs_part_3_5 = row.get('cognitive_style.1.player.cs_part3_5')
                p.cs_part_3_6 = row.get('cognitive_style.1.player.cs_part3_6')
                p.cs_part_3_7 = row.get('cognitive_style.1.player.cs_part3_7')
                p.cs_part_3_8 = row.get('cognitive_style.1.player.cs_part3_8')
                p.cs_part_3_9 = row.get('cognitive_style.1.player.cs_part3_9')
                p.cs_part_3_10 = row.get('cognitive_style.1.player.cs_part3_A')
                p.cs_part_3_11 = row.get('cognitive_style.1.player.cs_part3_B')

                cs2 = [int(p.cs_part_2_1) if p.cs_part_2_1 and p.cs_part_2_1 != '' else 0,
                       int(p.cs_part_2_2) if p.cs_part_2_2 and p.cs_part_2_2 != '' else 0,
                       int(p.cs_part_2_3) if p.cs_part_2_3 and p.cs_part_2_3 != '' else 0,
                       int(p.cs_part_2_4) if p.cs_part_2_4 and p.cs_part_2_4 != '' else 0,
                       int(p.cs_part_2_5) if p.cs_part_2_5 and p.cs_part_2_5 != '' else 0,
                       int(p.cs_part_2_6) if p.cs_part_2_6 and p.cs_part_2_6 != '' else 0,
                       int(p.cs_part_2_7) if p.cs_part_2_7 and p.cs_part_2_7 != '' else 0,
                       int(p.cs_part_2_8) if p.cs_part_2_8 and p.cs_part_2_8 != '' else 0,
                       int(p.cs_part_2_9) if p.cs_part_2_9 and p.cs_part_2_9 != '' else 0,
                       int(p.cs_part_2_10) if p.cs_part_2_10 and p.cs_part_2_10 != '' else 0]

                cs3 = [int(p.cs_part_3_1) if p.cs_part_3_1 and p.cs_part_3_1 != '' else 0,
                       int(p.cs_part_3_2) if p.cs_part_3_2 and p.cs_part_3_2 != '' else 0,
                       int(p.cs_part_3_3) if p.cs_part_3_3 and p.cs_part_3_3 != '' else 0,
                       int(p.cs_part_3_4) if p.cs_part_3_4 and p.cs_part_3_4 != '' else 0,
                       int(p.cs_part_3_5) if p.cs_part_3_5 and p.cs_part_3_5 != '' else 0,
                       int(p.cs_part_3_6) if p.cs_part_3_6 and p.cs_part_3_6 != '' else 0,
                       int(p.cs_part_3_7) if p.cs_part_3_7 and p.cs_part_3_7 != '' else 0,
                       int(p.cs_part_3_8) if p.cs_part_3_8 and p.cs_part_3_8 != '' else 0,
                       int(p.cs_part_3_9) if p.cs_part_3_9 and p.cs_part_3_9 != '' else 0,
                       int(p.cs_part_3_10) if p.cs_part_3_10 and p.cs_part_3_10 != '' else 0]

                p.cs = sum(cs2) + sum(cs3)

                p.ioc_1 = row.get('illusion_of_control.1.player.ioc_1')
                p.ioc_2 = row.get('illusion_of_control.1.player.ioc_2')
                p.ioc_3 = row.get('illusion_of_control.1.player.ioc_3')
                p.ioc_4 = row.get('illusion_of_control.1.player.ioc_4')
                p.ioc_5 = row.get('illusion_of_control.1.player.ioc_5')
                p.ioc_6 = row.get('illusion_of_control.1.player.ioc_6')
                p.ioc_7 = row.get('illusion_of_control.1.player.ioc_7')
                p.ioc_8 = row.get('illusion_of_control.1.player.ioc_8')
                p.ioc_9 = row.get('illusion_of_control.1.player.ioc_9')
                p.ioc_10 = row.get('illusion_of_control.1.player.ioc_10')

                p.ioc_11 = row.get('illusion_of_control.1.player.ioc_11')
                p.ioc_12 = row.get('illusion_of_control.1.player.ioc_12')

                ioc = [int(p.ioc_1) if p.ioc_1 and p.ioc_1 != '' else 0,
                       int(p.ioc_2) if p.ioc_2 and p.ioc_2 != '' else 0,
                       int(p.ioc_3) if p.ioc_3 and p.ioc_3 != '' else 0,
                       int(p.ioc_4) if p.ioc_4 and p.ioc_4 != '' else 0,
                       int(p.ioc_5) if p.ioc_5 and p.ioc_5 != '' else 0,
                       int(p.ioc_6) if p.ioc_6 and p.ioc_6 != '' else 0,
                       int(p.ioc_7) if p.ioc_7 and p.ioc_7 != '' else 0,
                       int(p.ioc_8) if p.ioc_8 and p.ioc_8 != '' else 0,
                       int(p.ioc_9) if p.ioc_9 and p.ioc_9 != '' else 0,
                       int(p.ioc_10) if p.ioc_10 and p.ioc_10 != '' else 0,
                       int(p.ioc_11) if p.ioc_11 and p.ioc_11 != '' else 0,
                       int(p.ioc_12) if p.ioc_12 and p.ioc_12 != '' else 0]
                p.ioc = sum(ioc)

                p.birth_date = row.get('payer_info.1.player.birthday')
                p.gender = 1 if row.get('payer_info.1.player.gender') == '男' else 0
                p.education = row.get('payer_info.1.player.education')
                p.investment_year = row.get('payer_info.1.player.invest_year')[:4]
                p.investment_experience = row.get('payer_info.1.player.experience')

                p.contact_info = row.get('payer_info.1.player.contact_information')

                age = 2025 - int(p.birth_date[:4])
                p.age = age

                if p.education_cn == '初中及以下':
                    p.education = 0
                elif p.education_cn == '普高/中专/技校/职高':
                    p.education = 1
                elif p.education_cn == '专科':
                    p.education = 2
                elif p.education_cn == '本科':
                    p.education = 3
                elif p.education_cn == '硕士及以上':
                    p.education = 4

                participants.append(p)

        return participants

    @staticmethod
    def parse_from_csv_without_advice(csv_file):
        """
        从CSV文件解析参与者数据并返回Participant对象列表
        :param csv_file: CSV文件路径
        :return: 包含所有Participant对象的列表
        """
        import csv
        participants = []

        with open(csv_file, 'r', encoding='utf-8') as f:
            reader = csv.DictReader(f)
            for row in reader:
                p = Participant()

                # 解析基础信息
                p.index_in_pages = row.get('participant._index_in_pages')
                p.index_in_pages_name = row.get('participant._current_page_name')

                p.payer_no = row.get('gambler_fallacy_id.1.player.payer_no')
                if not p.payer_no or p.index_in_pages_name != 'CsEnd':
                    continue

                p.gf_up_curr_1 = row.get('gambler_fallacy_kline.1.player.gf_up_curr')
                p.gf_up_curr_2 = row.get('gambler_fallacy_kline.2.player.gf_up_curr')
                p.gf_up_curr_3 = row.get('gambler_fallacy_kline.3.player.gf_up_curr')
                p.gf_up_curr_4 = row.get('gambler_fallacy_kline.4.player.gf_up_curr')
                p.gf_up_curr_5 = row.get('gambler_fallacy_kline.5.player.gf_up_curr')
                p.gf_up_curr_6 = row.get('gambler_fallacy_kline.6.player.gf_up_curr')
                p.gf_up_curr_7 = row.get('gambler_fallacy_kline.7.player.gf_up_curr')
                p.gf_up_curr_8 = row.get('gambler_fallacy_kline.8.player.gf_up_curr')
                p.gf_up_curr_9 = row.get('gambler_fallacy_kline.9.player.gf_up_curr')
                p.gf_up_curr_10 = row.get('gambler_fallacy_kline.10.player.gf_up_curr')

                p.gf_up_next_1 = row.get('gambler_fallacy_kline.1.player.gf_up_next')
                p.gf_up_next_2 = row.get('gambler_fallacy_kline.2.player.gf_up_next')
                p.gf_up_next_3 = row.get('gambler_fallacy_kline.3.player.gf_up_next')
                p.gf_up_next_4 = row.get('gambler_fallacy_kline.4.player.gf_up_next')
                p.gf_up_next_5 = row.get('gambler_fallacy_kline.5.player.gf_up_next')
                p.gf_up_next_6 = row.get('gambler_fallacy_kline.6.player.gf_up_next')
                p.gf_up_next_7 = row.get('gambler_fallacy_kline.7.player.gf_up_next')
                p.gf_up_next_8 = row.get('gambler_fallacy_kline.8.player.gf_up_next')
                p.gf_up_next_9 = row.get('gambler_fallacy_kline.9.player.gf_up_next')
                p.gf_up_next_10 = row.get('gambler_fallacy_kline.10.player.gf_up_next')

                p.gf_down_curr_1 = row.get('gambler_fallacy_kline.1.player.gf_dw_curr')
                p.gf_down_curr_2 = row.get('gambler_fallacy_kline.2.player.gf_dw_curr')
                p.gf_down_curr_3 = row.get('gambler_fallacy_kline.3.player.gf_dw_curr')
                p.gf_down_curr_4 = row.get('gambler_fallacy_kline.4.player.gf_dw_curr')
                p.gf_down_curr_5 = row.get('gambler_fallacy_kline.5.player.gf_dw_curr')
                p.gf_down_curr_6 = row.get('gambler_fallacy_kline.6.player.gf_dw_curr')
                p.gf_down_curr_7 = row.get('gambler_fallacy_kline.7.player.gf_dw_curr')
                p.gf_down_curr_8 = row.get('gambler_fallacy_kline.8.player.gf_dw_curr')
                p.gf_down_curr_9 = row.get('gambler_fallacy_kline.9.player.gf_dw_curr')
                p.gf_down_curr_10 = row.get('gambler_fallacy_kline.10.player.gf_dw_curr')

                p.gf_down_next_1 = row.get('gambler_fallacy_kline.1.player.gf_dw_next')
                p.gf_down_next_2 = row.get('gambler_fallacy_kline.2.player.gf_dw_next')
                p.gf_down_next_3 = row.get('gambler_fallacy_kline.3.player.gf_dw_next')
                p.gf_down_next_4 = row.get('gambler_fallacy_kline.4.player.gf_dw_next')
                p.gf_down_next_5 = row.get('gambler_fallacy_kline.5.player.gf_dw_next')
                p.gf_down_next_6 = row.get('gambler_fallacy_kline.6.player.gf_dw_next')
                p.gf_down_next_7 = row.get('gambler_fallacy_kline.7.player.gf_dw_next')
                p.gf_down_next_8 = row.get('gambler_fallacy_kline.8.player.gf_dw_next')
                p.gf_down_next_9 = row.get('gambler_fallacy_kline.9.player.gf_dw_next')
                p.gf_down_next_10 = row.get('gambler_fallacy_kline.10.player.gf_dw_next')

                gf = [p.gf_up_curr_1 is not None and p.gf_up_next_1 is not None and p.gf_up_curr_1 != p.gf_up_next_1,
                      p.gf_up_curr_2 is not None and p.gf_up_next_2 is not None and p.gf_up_curr_2 != p.gf_up_next_2,
                      p.gf_up_curr_3 is not None and p.gf_up_next_3 is not None and p.gf_up_curr_3 != p.gf_up_next_3,
                      p.gf_up_curr_4 is not None and p.gf_up_next_4 is not None and p.gf_up_curr_4 != p.gf_up_next_4,
                      p.gf_up_curr_5 is not None and p.gf_up_next_5 is not None and p.gf_up_curr_5 != p.gf_up_next_5,
                      p.gf_up_curr_6 is not None and p.gf_up_next_6 is not None and p.gf_up_curr_6 != p.gf_up_next_6,
                      p.gf_up_curr_7 is not None and p.gf_up_next_7 is not None and p.gf_up_curr_7 != p.gf_up_next_7,
                      p.gf_up_curr_8 is not None and p.gf_up_next_8 is not None and p.gf_up_curr_8 != p.gf_up_next_8,
                      p.gf_up_curr_9 is not None and p.gf_up_next_9 is not None and p.gf_up_curr_9 != p.gf_up_next_9,
                      p.gf_up_curr_10 is not None and p.gf_up_next_10 is not None and p.gf_up_curr_10 != p.gf_up_next_10,
                      p.gf_down_curr_1 is not None and p.gf_down_next_1 is not None and p.gf_down_curr_1 != p.gf_down_next_1,
                      p.gf_down_curr_2 is not None and p.gf_down_next_2 is not None and p.gf_down_curr_2 != p.gf_down_next_2,
                      p.gf_down_curr_3 is not None and p.gf_down_next_3 is not None and p.gf_down_curr_3 != p.gf_down_next_3,
                      p.gf_down_curr_4 is not None and p.gf_down_next_4 is not None and p.gf_down_curr_4 != p.gf_down_next_4,
                      p.gf_down_curr_5 is not None and p.gf_down_next_5 is not None and p.gf_down_curr_5 != p.gf_down_next_5,
                      p.gf_down_curr_6 is not None and p.gf_down_next_6 is not None and p.gf_down_curr_6 != p.gf_down_next_6,
                      p.gf_down_curr_7 is not None and p.gf_down_next_7 is not None and p.gf_down_curr_7 != p.gf_down_next_7,
                      p.gf_down_curr_8 is not None and p.gf_down_next_8 is not None and p.gf_down_curr_8 != p.gf_down_next_8,
                      p.gf_down_curr_9 is not None and p.gf_down_next_9 is not None and p.gf_down_curr_9 != p.gf_down_next_9,
                      p.gf_down_curr_10 is not None and p.gf_down_next_10 is not None and p.gf_down_curr_10 != p.gf_down_next_10]
                p.gf = sum(gf)

                p.cs_part_2_1 = row.get('cognitive_style.1.player.cs_part2_1')
                p.cs_part_2_2 = row.get('cognitive_style.1.player.cs_part2_2')
                p.cs_part_2_3 = row.get('cognitive_style.1.player.cs_part2_3')
                p.cs_part_2_4 = row.get('cognitive_style.1.player.cs_part2_4')
                p.cs_part_2_5 = row.get('cognitive_style.1.player.cs_part2_5')
                p.cs_part_2_6 = row.get('cognitive_style.1.player.cs_part2_6')
                p.cs_part_2_7 = row.get('cognitive_style.1.player.cs_part2_7')
                p.cs_part_2_8 = row.get('cognitive_style.1.player.cs_part2_8')
                p.cs_part_2_9 = row.get('cognitive_style.1.player.cs_part2_9')
                p.cs_part_2_10 = row.get('cognitive_style.1.player.cs_part2_A')
                p.cs_part_2_11 = row.get('cognitive_style.1.player.cs_part2_B')

                p.cs_part_3_1 = row.get('cognitive_style.1.player.cs_part3_1')
                p.cs_part_3_2 = row.get('cognitive_style.1.player.cs_part3_2')
                p.cs_part_3_3 = row.get('cognitive_style.1.player.cs_part3_3')
                p.cs_part_3_4 = row.get('cognitive_style.1.player.cs_part3_4')
                p.cs_part_3_5 = row.get('cognitive_style.1.player.cs_part3_5')
                p.cs_part_3_6 = row.get('cognitive_style.1.player.cs_part3_6')
                p.cs_part_3_7 = row.get('cognitive_style.1.player.cs_part3_7')
                p.cs_part_3_8 = row.get('cognitive_style.1.player.cs_part3_8')
                p.cs_part_3_9 = row.get('cognitive_style.1.player.cs_part3_9')
                p.cs_part_3_10 = row.get('cognitive_style.1.player.cs_part3_A')
                p.cs_part_3_11 = row.get('cognitive_style.1.player.cs_part3_B')

                cs2 = [int(p.cs_part_2_1) if p.cs_part_2_1 and p.cs_part_2_1 != '' else 0,
                       int(p.cs_part_2_2) if p.cs_part_2_2 and p.cs_part_2_2 != '' else 0,
                       int(p.cs_part_2_3) if p.cs_part_2_3 and p.cs_part_2_3 != '' else 0,
                       int(p.cs_part_2_4) if p.cs_part_2_4 and p.cs_part_2_4 != '' else 0,
                       int(p.cs_part_2_5) if p.cs_part_2_5 and p.cs_part_2_5 != '' else 0,
                       int(p.cs_part_2_6) if p.cs_part_2_6 and p.cs_part_2_6 != '' else 0,
                       int(p.cs_part_2_7) if p.cs_part_2_7 and p.cs_part_2_7 != '' else 0,
                       int(p.cs_part_2_8) if p.cs_part_2_8 and p.cs_part_2_8 != '' else 0,
                       int(p.cs_part_2_9) if p.cs_part_2_9 and p.cs_part_2_9 != '' else 0,
                       int(p.cs_part_2_10) if p.cs_part_2_10 and p.cs_part_2_10 != '' else 0]

                cs3 = [int(p.cs_part_3_1) if p.cs_part_3_1 and p.cs_part_3_1 != '' else 0,
                       int(p.cs_part_3_2) if p.cs_part_3_2 and p.cs_part_3_2 != '' else 0,
                       int(p.cs_part_3_3) if p.cs_part_3_3 and p.cs_part_3_3 != '' else 0,
                       int(p.cs_part_3_4) if p.cs_part_3_4 and p.cs_part_3_4 != '' else 0,
                       int(p.cs_part_3_5) if p.cs_part_3_5 and p.cs_part_3_5 != '' else 0,
                       int(p.cs_part_3_6) if p.cs_part_3_6 and p.cs_part_3_6 != '' else 0,
                       int(p.cs_part_3_7) if p.cs_part_3_7 and p.cs_part_3_7 != '' else 0,
                       int(p.cs_part_3_8) if p.cs_part_3_8 and p.cs_part_3_8 != '' else 0,
                       int(p.cs_part_3_9) if p.cs_part_3_9 and p.cs_part_3_9 != '' else 0,
                       int(p.cs_part_3_10) if p.cs_part_3_10 and p.cs_part_3_10 != '' else 0]

                p.cs = sum(cs2) + sum(cs3)

                p.ioc_1 = row.get('illusion_of_control.1.player.ioc_1')
                p.ioc_2 = row.get('illusion_of_control.1.player.ioc_2')
                p.ioc_3 = row.get('illusion_of_control.1.player.ioc_3')
                p.ioc_4 = row.get('illusion_of_control.1.player.ioc_4')
                p.ioc_5 = row.get('illusion_of_control.1.player.ioc_5')
                p.ioc_6 = row.get('illusion_of_control.1.player.ioc_6')
                p.ioc_7 = row.get('illusion_of_control.1.player.ioc_7')
                p.ioc_8 = row.get('illusion_of_control.1.player.ioc_8')
                p.ioc_9 = row.get('illusion_of_control.1.player.ioc_9')
                p.ioc_10 = row.get('illusion_of_control.1.player.ioc_10')

                p.ioc_11 = row.get('illusion_of_control.1.player.ioc_11')
                p.ioc_12 = row.get('illusion_of_control.1.player.ioc_12')

                ioc = [int(p.ioc_1) if p.ioc_1 and p.ioc_1 != '' else 0,
                       int(p.ioc_2) if p.ioc_2 and p.ioc_2 != '' else 0,
                       int(p.ioc_3) if p.ioc_3 and p.ioc_3 != '' else 0,
                       int(p.ioc_4) if p.ioc_4 and p.ioc_4 != '' else 0,
                       int(p.ioc_5) if p.ioc_5 and p.ioc_5 != '' else 0,
                       int(p.ioc_6) if p.ioc_6 and p.ioc_6 != '' else 0,
                       int(p.ioc_7) if p.ioc_7 and p.ioc_7 != '' else 0,
                       int(p.ioc_8) if p.ioc_8 and p.ioc_8 != '' else 0,
                       int(p.ioc_9) if p.ioc_9 and p.ioc_9 != '' else 0,
                       int(p.ioc_10) if p.ioc_10 and p.ioc_10 != '' else 0,
                       int(p.ioc_11) if p.ioc_11 and p.ioc_11 != '' else 0,
                       int(p.ioc_12) if p.ioc_12 and p.ioc_12 != '' else 0]
                p.ioc = sum(ioc)

                p.birth_date = row.get('payer_info.1.player.birthday')
                p.gender = 1 if row.get('payer_info.1.player.gender') == '男' else 0
                p.education_cn = row.get('payer_info.1.player.education')
                p.investment_year = row.get('payer_info.1.player.invest_year')[:4]
                p.investment_experience = row.get('payer_info.1.player.experience')

                p.contact_info = row.get('payer_info.1.player.contact_information')

                age = 2025 - int(p.birth_date[:4])
                p.age = age

                if p.education_cn == '初中及以下':
                    p.education = 0
                elif p.education_cn == '普高/中专/技校/职高':
                    p.education = 1
                elif p.education_cn == '专科':
                    p.education = 2
                elif p.education_cn == '本科':
                    p.education = 3
                elif p.education_cn == '硕士及以上':
                    p.education = 4

                participants.append(p)

        return participants

    @staticmethod
    def save_to_csv(participants, filename):
        import csv

        # 定义CSV文件的表头
        fieldnames = [
            'index_in_pages', 'index_in_pages_name', 'payer_no',
            'gf_up_curr_1', 'gf_up_curr_2', 'gf_up_curr_3', 'gf_up_curr_4', 'gf_up_curr_5',
            'gf_up_curr_6', 'gf_up_curr_7', 'gf_up_curr_8', 'gf_up_curr_9', 'gf_up_curr_10',
            'gf_up_next_1', 'gf_up_next_2', 'gf_up_next_3', 'gf_up_next_4', 'gf_up_next_5',
            'gf_up_next_6', 'gf_up_next_7', 'gf_up_next_8', 'gf_up_next_9', 'gf_up_next_10',
            'gf_down_curr_1', 'gf_down_curr_2', 'gf_down_curr_3', 'gf_down_curr_4', 'gf_down_curr_5',
            'gf_down_curr_6', 'gf_down_curr_7', 'gf_down_curr_8', 'gf_down_curr_9', 'gf_down_curr_10',
            'gf_down_next_1', 'gf_down_next_2', 'gf_down_next_3', 'gf_down_next_4', 'gf_down_next_5',
            'gf_down_next_6', 'gf_down_next_7', 'gf_down_next_8', 'gf_down_next_9', 'gf_down_next_10',
            'listen_advice', 'listen_advice_help',
            'cs_part_2_1', 'cs_part_2_2', 'cs_part_2_3', 'cs_part_2_4', 'cs_part_2_5',
            'cs_part_2_6', 'cs_part_2_7', 'cs_part_2_8', 'cs_part_2_9', 'cs_part_2_10', 'cs_part_2_11',
            'cs_part_3_1', 'cs_part_3_2', 'cs_part_3_3', 'cs_part_3_4', 'cs_part_3_5',
            'cs_part_3_6', 'cs_part_3_7', 'cs_part_3_8', 'cs_part_3_9', 'cs_part_3_10', 'cs_part_3_11',
            'ioc_1', 'ioc_2', 'ioc_3', 'ioc_4', 'ioc_5', 'ioc_6', 'ioc_7', 'ioc_8', 'ioc_9', 'ioc_10',
            'ioc_11', 'ioc_12',
            'gf', 'cs', 'ioc',
            'birth_date', 'age', 'gender', 'education_cn', 'investment_year', 'investment_experience',
            'contact_info'
        ]

        # 打开文件并写入数据
        with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()

            # 遍历每个参与者对象
            for p in participants:
                # 将对象属性转换为字典
                row = {field: getattr(p, field) for field in fieldnames}
                writer.writerow(row)


# 听取意见组的csv文件
def process_advice_group(input_paths, output_path):
    """
    听取意见组数据
    :param input_paths: 输入文件路径列表
    :param output_path: 输出文件路径
    """
    participants = []
    for path in input_paths:
        participants.extend(Participant.parse_from_csv(path))

    print(f"听取意见组参与者数量: {len(participants)}")

    # 保存为csv文件
    Participant.save_to_csv(participants, output_path)


def process_no_advice_group(input_paths, output_path):
    """
    独立决策组数据
    :param input_paths: 输入文件路径列表
    :param output_path: 输出文件路径
    """
    participants = []
    for path in input_paths:
        participants.extend(Participant.parse_from_csv_without_advice(path))

    print(f"独立决策组参与者数量: {len(participants)}")

    # 保存为csv文件
    Participant.save_to_csv(participants, output_path)


paths = ["/Users/linwenyu/Downloads/all_apps_wide-2025-03-04.csv",
         "/Users/linwenyu/Downloads/all_apps_wide-2025-03-04 (1).csv",
         ]
output_path = "/Users/linwenyu/Downloads/中财课程/投资者行为与心理研究/开题/实验2/听取意见组.csv"
process_advice_group(paths, output_path)

paths = ["/Users/linwenyu/Downloads/all_apps_wide-2025-03-04 (2).csv",
         "/Users/linwenyu/Downloads/all_apps_wide-2025-03-04 (3).csv",
         ]
output_path = "/Users/linwenyu/Downloads/中财课程/投资者行为与心理研究/开题/实验2/独立决策组.csv"
process_no_advice_group(paths, output_path)
